package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

/**
 * Render the list created flashcardset at dashboard
 * @author ocsen
 *
 */
public class DashboardCreatedListVm {
	
	private List<FlashcardSet> list = null;
	public static final int PAGE_SIZE = 12;
	private int page = 0;
	private int total = 0;
	
	private User user;//current user
	private DashboardCtrl ctrl = null;

	@Init
	public void init(@ContextParam(ContextType.COMPONENT) Component com) {
		ctrl = (DashboardCtrl) com.getRoot().getFellow("dbctrl").getAttribute("$composer");
		
		this.user = ctrl.getUser();
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		total = srv.countCreatedFlashcardsets(getUser());
		loadList();
	}
	
	@NotifyChange("list")
	@Command
	public void paging(@BindingParam("event") PagingEvent event) {
		page = event.getActivePage();
		loadList();
	}
	
	/**
	 * Update the created list
	 */
	@NotifyChange("list")
	@GlobalCommand
	public void updateCreatedList() {
		page = 0;
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		setList(srv.getCreatedFlashcardsets(getUser(), page, PAGE_SIZE));
	}
	
	public void loadList() {
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		setList(srv.getCreatedFlashcardsets(getUser(), page, PAGE_SIZE));
	}
	
	public int getNumForTesting(FlashcardSet fcs) {
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		return srv.getNumOfWaitingWordForTest(fcs);
	}
	
	public int getNumKnowCards(FlashcardSet fcs) {
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		return srv.getNumOfKnowCards(fcs);
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the list
	 */
	public List<FlashcardSet> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<FlashcardSet> list) {
		this.list = list;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}
}
